package org.brisling.sysman.queryService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.brisling.base.domain.ListDomain;
import org.brisling.base.service.AbstractBaseQueryService;
import org.brisling.common.ConstSysParam;
import org.brisling.common.util.LongUtil;
import org.brisling.common.util.WhereUtil;
import org.brisling.sysman.domain.JobRole;
import org.brisling.sysman.domain.JobType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

@RestController
@RequestMapping("/jobrole/query")
public class QueryJobRoleService extends AbstractBaseQueryService<JobRole>{
	
	
	
	public QueryJobRoleService(){
		this.moduleName = ConstSysParam.MODULE_JOB_ROLE;
		this.subSystem = ConstSysParam.SUBSYSTEM_SYSMAN;
		this.tableName = ConstSysParam.VIEW_PREFIX +ConstSysParam.MODULE_JOB_ROLE;
		this.clazz = JobRole.class;
		typeMap = this.getTypeMap(clazz);
				
		orderMap.put("sortedid","");			
		
	}
	
	@PersistenceContext(unitName=ConstSysParam.SUBSYSTEM_SYSMAN)
	@Autowired
	protected void setEntityManager(EntityManager entityManager){
		this.entityManager = entityManager;		
	}
	
	@RequestMapping("/mainPage.action")
	public ModelAndView getMainPage(){
		return new ModelAndView("/sysman/sysman_role/main");
	}
	
	/* (non-Javadoc)
	 * @see org.brisling.service.sysman.DepartmentServiceInf#getEditPage()
	 */
	@RequestMapping("/editPage.action")
	public ModelAndView getEditPage(){
		return new ModelAndView("/sysman/sysman_user_role/editJobRoleDlg");
	}
	
	@RequestMapping("/getListDomain.action")
	public ListDomain<JobRole> getListDomain(@RequestBody(required=false) String body){		
		
		return super.getListDomainByConditionAndOrder(body, typeMap, orderMap, true);
		
	}
	
	/**
	 * 获取人员角色
	 * @param userid
	 * @return
	 */
	@RequestMapping("/getJobRoleByUseridListDomain.action")
	public ListDomain<JobRole> getJobRoleByUseridListDomain(@RequestParam(value="userid",required=false)String userid){
		if(userid!=null && userid.trim().length()>0){
			Map<String,Object> map = new HashMap<String,Object>();
			Map<String,String> paramMap = new HashMap<String,String>();
			paramMap.put("userid", userid);
			map.put(ConstSysParam.CONDITION_MAP_KEY, paramMap);
			return this.getListDomain(map);
		}else{
			return new ListDomain<JobRole>();
		}
		
	}
	
	/**
	 * 获取人员角色
	 * @param map
	 * @return
	 */
	public ListDomain<JobRole> getListDomain(Map<String,Object> map){	
		
		return super.getListDomainByConditionAndOrder(null,map, typeMap, orderMap, true);
		
	}
	
	
	
	@RequestMapping("/getTypeListDomain.action")
	public ListDomain<Object> getTypeListDomain(@RequestBody(required=false) String body){		
		
		String sql = String.format("select * from %1$s ",ConstSysParam.MODULE_JOBTYPE) ;
		return super.getPagedClassListDomain(sql, body, JobType.class, JobType.class,true);
		
		
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping("/getList.action")
	public List<JobRole> getList(@RequestBody(required=false) String body){		
		
		if(body!=null){
			Map<String,Object> map =getBodyParam(body);			
			return getList((Map<String, String>) map.get("condition"),true);			
		}		
		return null;
	}	
	
	public List<JobRole> getList(Map<String,String> map){		
		
		if(map!=null){					
			return getList(map,true);			
		}		
		return null;
	}
	
	/**
	 * 获取科室指定岗位角色人员列表
	 * @param roleNames
	 * @return
	 */
	public Map<Long,JobRole> getDeptJobRoles(String roleNames){
		Map<Long,JobRole> jobMap = new HashMap<Long,JobRole>();
		
		Map<String,String> paramMap = new HashMap<String,String>();
		paramMap.put("jobname", roleNames);
		List<JobRole> jobRoles =this.getList(paramMap,false);
		
		if(jobRoles!=null && jobRoles.size()>0){
			for(JobRole j:jobRoles){
				jobMap.put(j.getDeptid(), j);
			}
		}		
		return jobMap;		
	}
	
	/**
	 * 获取科室指定岗位角色人员列表
	 * @param roleNames
	 * @return
	 */
	public Map<String,JobRole> getDeptNameJobRoles(String roleNames){
		Map<String,JobRole> jobMap = new HashMap<String,JobRole>();
		
		Map<String,String> paramMap = new HashMap<String,String>();
		paramMap.put("jobname", roleNames);
		List<JobRole> jobRoles =this.getList(paramMap,false);
		
		if(jobRoles!=null && jobRoles.size()>0){
			for(JobRole j:jobRoles){
				String _name = j.getDeptname();
				if(_name!=null){
					int index = _name.indexOf("-");
					if(index>=0){
						_name = _name.substring(0,index);
					}
					
					index = _name.indexOf("-");
					if(index>=0){
						_name = _name.substring(0,index);
					}
				}
				jobMap.put(_name, j);
			}
		}		
		return jobMap;		
	}
	
	
	
	/**
	 * 查询列表集合数据
	 * @param map	查询条件设置
	 * @param pageable	分页标志
	 * @return
	 */
	public List<JobRole> getList(Map<String, String> map,Boolean pageable){	
		
		String sql = "";
		String whereCorse = null;					
			
		whereCorse = WhereUtil.getConditionWhereCorse(map,typeMap);
		System.out.println("whereCorse: " +whereCorse);
		
		Integer page = map.containsKey("page")?LongUtil.getIntValue(map.get("page").toString(),null):ConstSysParam.PAGE;	
		Integer rows = map.containsKey("rows")?LongUtil.getIntValue(map.get("rows").toString(),null):ConstSysParam.ROWS;
		
		if(whereCorse!=null){			
			sql = String.format(this.TW_SQL, this.tableName,whereCorse);			
		}else{
			sql = String.format(this.T_SQL, this.tableName);
		}
		
		String orderCorse = null;
		orderCorse = getOrderStr(orderMap);
		if(orderCorse!=null){
			sql = sql + " order by " +orderCorse;
		}		
		if(pageable){
			return super.getPagedList(page,rows,sql);
		}else{
			return super.getAllList(sql);
		}
	}
	@RequestMapping("/getListAll.action")
	public List<JobRole> getListAll(){	
		
		String sql = String.format(this.T_SQL, this.tableName);
		return super.getAllList(sql);	
	}
	
	@RequestMapping("/getTypeListAll.action")
	public List<JobType> getTypeListAll(){	
		
		String sql = String.format(this.T_SQL, ConstSysParam.MODULE_JOBTYPE);
		return super.reflectClassedtMaps(JobType.class,super.getMappedList(sql));	
	}
	
	/**
	 * 根据用户id查找用户信息
	 * @param id	用户id
	 * @return 用户信息记录 
	 */
	@RequestMapping("/getOne.action")
	public JobRole getOne(@RequestParam(value="id",required=false) Long id){
		return super.getOne(id);
	}
	
	/**
	 * 根据用户唯一编码查找用户信息
	 * @param identify	用户唯一编码
	 * @return 用户信息记录
	 */
	@RequestMapping("/getOneByIdentify.action")
	public JobRole getOneByIdentify(@RequestParam(value="identify",required=false) String identify){	
		return super.getOneByIdentify(identify);
	}	
	
	
	@RequestMapping("/getListByField.action")
	public List<JobRole> getListByField(@RequestParam(value="field",required=false) String field,@RequestParam(value="fieldVal",required=false) String fieldVal){	
		return super.getListByIdentify(field,fieldVal);
	}
}
